<?php
  require_once('includes/db.php');
  require_once('includes/header.php');
  $url = $_SERVER['PHP_SELF'];
  $uid = isset($_GET['id'])?$_GET['id']:0;
  ############################
  $rpp = 3 ; // rpp=>record per page
  $page = isset($_GET['page'])?$_GET['page']:1; // get page no. from url
  $totalUsersCount = countAll('users'); // count total records of table
  $index = ($page -1 ) * $rpp;
  ##################################
  ## Update Status ##
  if(isset($_GET['status'])){
    $newStatus = $_GET['status'];
    $data = array("status"=>$newStatus);
    updateById('users',$data,$uid);
  }
?>
  <!-- Main component for a primary marketing message or call to action -->
  <div class="jumbotron" style="padding: 10px;">
    <h4>Show All Users</h4>
  </div>
<?php
  ############################
  $result = fetchAll('users','','','',"LIMIT $index,$rpp");// fetchall is in db.php
  $totalPages = ceil($totalUsersCount/$rpp);
  pr($totalPages,__LINE__,__FILE__);
  ############################
?>


<table  class="table table-bordered table-striped table-hover">
  <thead>
    <tr>
      <th>UID</th>
      <th>Name</th>
      <th>Email</th>
      <th>Gender</th>
      <th>D.O.B.</th>
      <th>Avatar</th>
      <th>Added date</th>
      <th>Status</th>
    </tr>
  </thead>
  <tbody>

<?php
while($ans = mysql_fetch_assoc($result)) {
  $id = $ans['id'];
  $toggleStatus = toggleStatus($ans['status']);
  $status_url = $url."?status=$toggleStatus&id=$id&page=$page"
?>
    <tr>
      <td ><a target="_blank" href="edit_user.php?id=<?=$id?>"><?=$id?></a></td>
      <td ><?=$ans['uname']?></td>
      <td ><a href="mailto:<?=$ans['email']?>"><?=$ans['email']?></a></td>
      <td ><?=getGender($ans['gender'])?></td>
      <td ><?=date('d-M-Y',strtotime($ans['dob']))?></td>
      <td >

      <a class="fancybox-effects-b" title="Lorem ipsum dolor sit amet, consectetur adipiscing elit"  href="uploads/<?=$ans['avatar']?>">
      <img src="uploads/thumbs_120_90/<?=$ans['avatar']?>">
      </a>

      </td>

      <td ><?=date('d-M-Y h:i a',strtotime($ans['added_date']))?></td>
      <td ><a title="<?=$ans['status']?>" style="font-size: 25px;" href="<?=$status_url?>"><?=getStatus($ans['status'])?></a></td>
    </tr>
<?php
} // end while
?>
  </tbody>
</table>

<ul class="pagination pagination-lg">
<?php
  if ($page == 1) {
?>
  <li class="disabled"><a href="#">&laquo;</a></li>
<?php
  } else {//end if
    $prev = $page - 1;
    echo "<li><a href=\"$url?page=$prev\">&laquo;</a></li>";
  }
//$currentPage = 1;
for($i=1 ; $i<=$totalPages  ; $i++)
{
  //$class = ($i==$currentPage) ? 'active':'';
  if($i==$page){
?>
  <li  class="active"><a ><?=$i?></a></li>
<?php
  } else {//end if
  echo "<li><a href=\"$url?page=$i\">$i</a></li>";

  }//end else
}
?>
<?php
  if ($page == $totalPages) {
?>
  <li class="disabled"><a href="#">&raquo;</a></li>
<?php
  } else {//end if
    $prev = $page + 1;
    echo "<li><a href=\"$url?page=$prev\">&raquo;</a></li>";
  }
?>
</ul>

<?php
   require_once('includes/footer.php');
?>
